package org.fnzn.web.rest;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.fnzn.bean.LrdCorp;
import org.fnzn.biz.LrdCorpBiz;
import org.fnzn.service.LrdUserService;
import org.fnzn.web.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.fnzn.aop.SystemControllerLog;
import org.fnzn.bean.vo.User;
import org.fnzn.core.Result;
import org.fnzn.util.CommonUtil;
import org.fnzn.util.ResultGenerator;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/admin")
@Slf4j
public class UserPermissionController {
    @Autowired
    private UserService userService;
    @Autowired
    private LrdUserService lrdUserService;
    @Autowired
    private LrdCorpBiz lrdCorpBiz;
    
    private final String getPicPath="/sysFile/getPic/";
    
    @RequestMapping(value = "/user/system",method = RequestMethod.GET)
    public Result getUserSystem(){
       User user=CommonUtil.getUser();
       return ResultGenerator.genSuccessResult(userService.getSystemsByUsername(user.getUser_id()));
    }
    @RequestMapping(value = "/user/menu",method = RequestMethod.GET)
    public String getUserMenu(@RequestParam(defaultValue = "-1") Integer parentId){
    	User user=CommonUtil.getUser();
        return JSONObject.toJSONString(userService.getMenusByUsername(user.getUser_id(),parentId));
    }
    @RequestMapping(value = "/user/pad/menu",method = RequestMethod.GET)
    public Result getUserPadMenu(@RequestParam(defaultValue = "-1") Integer parentId){
    	User user=CommonUtil.getUser();
    	if(user!=null){
    		return ResultGenerator.genSuccessResult(userService.getMenusByUsername(user.getUser_id(),parentId));
    	}else{
    		return ResultGenerator.genUNAUTHORResult();
    	}
    }
    @RequestMapping(value = "/user/updatePwd", produces = "application/json;charset=UTF-8", method = { RequestMethod.POST })
	@SystemControllerLog(description = "修改密码")
	public Result updatePwd(HttpServletRequest request,@RequestBody Map<String,Object> map){		
		return lrdUserService.updatePwd(request,map);
	}
    @RequestMapping(value = "/corp",method = RequestMethod.GET)
    @SystemControllerLog(description = "获取法人和法人logo信息")
    public Result getUserCorp(String corpCd,String host){  
       log.info("法人号：{},地址:{}",corpCd,host);	
       LrdCorp corp=new LrdCorp();
       corp.setCorpCd(corpCd);
       corp.setHost(host);
       corp=lrdCorpBiz.selectOne(corp);
       if(corp!=null){
	       corp.setLogo1(getPicPath+corp.getLogo1());
	       corp.setLogo2(getPicPath+corp.getLogo2());
	       corp.setLogo3(getPicPath+corp.getLogo3());
       }
       return ResultGenerator.genSuccessResult(corp);
    }    
}
